/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>Project: javaweb - CharFilter
 * <p>Powered by webrx On 2022-03-09 14:40:04
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
//@WebFilter(urlPatterns = {"/b.jsp","/c.jsp"})
public class CharFilter implements Filter {

    private String cs = "utf-8";
    private FilterConfig cfg;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.cfg = filterConfig;
        if(this.cfg.getInitParameter("cs") == null){

        }else{
            this.cs = this.cfg.getInitParameter("cs");
        }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain f) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        if("POST".equals(req.getMethod())) {
            req.setCharacterEncoding(this.cs);
        }

        System.out.println("CharFilter......");

        //通过过滤器
        f.doFilter(request,response);
    }

    @Override
    public void destroy() {
        this.cfg = null;
    }
}
